package com.cy.pj.common.aspect;


import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Aspect
@Component
public class SysCacheAspect {

    private Map<String,Object> cache=new ConcurrentHashMap<>();
    @Pointcut("@annotation(com.cy.pj.common.annotation.RequiredCache)")
    private void doCache(){}

    @Pointcut("@annotation(com.cy.pj.common.annotation.ClearCache)")
    private void doClearCache(){}

    @AfterReturning("doClearCache()")
    public void doAfterReturning(){
cache.clear();
    }


    @Around("doCache()")
    public Object around(ProceedingJoinPoint jp)
            throws Throwable{
        System.out.println("Get data from cache");
        Object result=cache.get("deptKey");
        if (result!=null) {
            return result;
        }
        result=jp.proceed();
        System.out.println("Put data to cache");
        return result;
    }

}
